import config from '../../utils/config';

const defaultAvatarUrl = config.defaultAvatarUrl

Page({
  data: {
    userInfo: {
      avatar: defaultAvatarUrl,
      name: '登录/注册',
      color: '#BAB7B7'
    },
    isLoggedIn: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    features: [
      {
        image: '/images/orderList.png',
        text: '订单查询',
        url: '/pages/seal/orderList/orderList'
      },{
        image: '/images/location.png',
        text: '地址管理',
        url: '/pages/address/address'
      }
    ],
    orderStatuses: [
      {
        image: '/images/orderStatus/pendingPayment.png',
        text: '待付款',
        url: '/pages/order/pendingPayment'
      },
      {
        image: '/images/orderStatus/pendingShipment.png',
        text: '待发货',
        url: '/pages/order/pendingShipment'
      },
      {
        image: '/images/orderStatus/pendingReceipt.png',
        text: '待收货',
        url: '/pages/order/pendingReceipt'
      },
      {
        image: '/images/orderStatus/pendingReview.png',
        text: '待评价',
        url: '/pages/order/pendingReview'
      },
      {
        image: '/images/orderStatus/refund.png',
        text: '退款/售后',
        url: '/pages/order/refund'
      }
    ]
  },

  // 功能跳转 function
  navigateToFeature: function(event) {
    this.checkLoginStatus();

    const url = event.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    });
  },

  // 订单状态
  navigateToOrderStatus: function(event) {
    this.checkLoginStatus();

    const status = event.currentTarget.dataset.status;
    const token = wx.getStorageSync('token');

    if (!token) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      wx.navigateTo({
        url: '/pages/login/login'
      });
      return;
    }

    // 跳转到订单列表页面，传递状态参数
    wx.navigateTo({
      url: `/pages/seal/orderList/orderList?status=${status}`
    });
  },
  onLoad() {},

  onShow() {
    const token         = wx.getStorageSync('token');
    const cacheUserInfo = wx.getStorageSync('userInfo');

    if (token) {
      this.setData({
        userInfo: {
          avatar: cacheUserInfo.avatar || defaultAvatarUrl,
          name: cacheUserInfo.name || '登录/注册',
          color: '#000'
        },
        isLoggedIn: true
      });
    } else {
      this.setData({
        isLoggedIn: false
      });
    }
    
    // if (!cacheUserInfo) {
    //   wx.request({
    //     url: `${config.baseUrl}/getUserInfo`,
    //     method: 'GET',
    //     header: {
    //       Authorization : token
    //     },
    //     success: (res) => {
    //       if (res.data && res.data.code === 200) {
    //         const userInfo = res.data.data;
            
    //         this.setData({
    //           userInfo: {
    //             avatar: userInfo.avatar || defaultAvatarUrl,
    //             name: userInfo.name || '',
    //             color: '#000'
    //           },
    //           isLoggedIn: true
    //         });
    //       } else {
    //         this.setData({
    //           userInfo: {
    //             avatar: defaultAvatarUrl,
    //             name: ''
    //           },
    //           isLoggedIn: false
    //         });
    //         console.error('获取用户信息失败:', res);
    //       }
    //     },
    //     fail: (err) => {
    //       this.setData({
    //         userInfo: {
    //           avatar: defaultAvatarUrl,
    //           name: ''
    //         },
    //         isLoggedIn: false
    //       });
    //       console.error('请求用户信息失败:', err);
    //     }
    //   });
    // } else {
    //   this.setData({
    //     userInfo: {
    //       avatar: cacheUserInfo.avatar || defaultAvatarUrl,
    //       name: cacheUserInfo.name || '登录/注册',
    //       color: '#000'
    //     },
    //     isLoggedIn: true
    //   });
    // }
  },

  // 检查用户是否登录  未登录 - 跳转到登录页
  checkLoginStatus: function() {
    const isLoggedIn = this.data.isLoggedIn;
    if (!isLoggedIn) {
      wx.navigateTo({
        url: '/pages/login/login'
      });
    }
  },
  
  // 设置头像以及用户名
  navigateToSetting: function() {
    this.checkLoginStatus();
    wx.navigateTo({
      url: '/pages/setting/setting'
    });
  }
})